﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;

namespace ViazyNetCore
{
    public class CurrentUser : ICurrentUser
    {
        public long Id => _principalAccessor.Principal?.GetUserId() ?? 0;

        public string Username => this.FindClaimValue(JwtClaimTypes.Name);

        public string Nickname => this.FindClaimValue(JwtClaimTypes.GivenName);

        public AuthUserType IdentityType => this.FindClaimValue<AuthUserType>(JwtClaimTypes.Type);

        public bool IsModerated => false;

        public long TenantId { get; set; }

        private readonly ICurrentPrincipalAccessor _principalAccessor;

        public CurrentUser(ICurrentPrincipalAccessor principalAccessor)
        {
            _principalAccessor = principalAccessor;
        }

        public virtual Claim FindClaim(string claimType)
        {
            return _principalAccessor.Principal?.Claims.FirstOrDefault(c => c.Type == claimType);
        }
    }
}
